package de.scheduler;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class Timer
{
   public static final long EIN_TAG = 24 * 60 * 60 *1000;
   public static final long EINE_STUNDE =  60 * 60 *1000;
   public static final long EINE_MINUTE =  60 *1000;
   
   SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
   private long defaultSleepTime =1000;
   
   private Date startTime;
   
   private Date stopTime;
   
   
   public Timer(long defaultIntervall)
   {
      this.defaultSleepTime = defaultIntervall;
   }
   
   public long getSleepTime()
   {
      if(isStartStop())
      {
         return nextStartStopTime();
      }
      new Date();
      
      return defaultSleepTime;
   }
   
   
   private long nextStartStopTime()
   {
      Date currenttime = new Date();
      
      long startInSecs = defaultSleepTime;
      
      if(currenttime.before(stopTime) && currenttime.after(startTime))
      {
         return defaultSleepTime;
      }
      else
      {
         startInSecs = startTime.getTime() - currenttime.getTime();
         
         if(startInSecs < 0 )
         {
            setNextStartTime(Timer.EIN_TAG);
            setNextStopTime(Timer.EIN_TAG);
            startInSecs = startTime.getTime() - currenttime.getTime();
            
            System.out.println("next Start Time "+new Date(System.currentTimeMillis()+startInSecs));
         }
         
         return startInSecs;
      }
   }


   private void setNextStartTime(long einTag)
   {
     startTime = new Date(startTime.getTime() + einTag);
   }

   private void setNextStopTime(long einTag)
   {
      startTime = new Date(startTime.getTime() + einTag);
      stopTime = new Date(stopTime.getTime() + einTag);
      
      System.out.println("next start Stop time "+df.format(startTime)+" "+df.format(stopTime));
   }

   public void setStopTime(String time)
   {
      try
      {
         stopTime = df.parse(time);
      }
      catch (ParseException e)
      {
         e.printStackTrace();
      }
   }
   
   public void setStopTime(long time)
   {
         stopTime = new Date(time);
         System.out.println("stop time ="+df.format(stopTime));
   }
   
   
   public void setStartTime(String time)
   {
      try
      {
         startTime = df.parse(time);
      }
      catch (ParseException e)
      {
         e.printStackTrace();
      }
   }
   
   
   public void setStartTime(long time)
   {
      startTime = new Date(time);
   }
   
  
   private boolean isStartStop()
   {
      if(startTime != null && stopTime != null)
         return true;
      
      return false;
   }

   public void setStartTime(int hour, int minute, int secs)
   {
      Calendar instance = Calendar.getInstance();
      instance.set(Calendar.HOUR, hour);
      instance.set(Calendar.MINUTE, minute);
      instance.set(Calendar.SECOND, secs);
      startTime = instance.getTime();
      
   }
   
   
   public void setStopTime(int hour, int minute, int secs)
   {
      Calendar instance = Calendar.getInstance();
      instance.set(Calendar.HOUR, hour);
      instance.set(Calendar.MINUTE, minute);
      instance.set(Calendar.SECOND, secs);
      stopTime = instance.getTime();
      
   }
}
